package com.airS.task.fileMgr.controller.home;

import com.airS.task.fileMgr.common.anno.Invoke;
import com.airS.task.fileMgr.common.constants.FilemgrConstants;
import com.airS.task.fileMgr.common.em.LogType;
import com.airS.task.fileMgr.common.response.BaseResponse;
import com.airS.task.fileMgr.common.response.ResponseCode;
import com.airS.task.fileMgr.controller.base.SupportController;
import com.airS.task.fileMgr.mapper.extra.gk.PermissionExtraMapper;
import com.airS.task.fileMgr.model.gk.Permission;
import com.airS.task.fileMgr.model.gk.PermissionTree;
import com.airS.task.fileMgr.security.shiro.filter.FormAuthenticationFilter;
import com.airS.task.fileMgr.security.shiro.realm.SystemAuthorizingRealm;
import com.airS.task.fileMgr.utils.shiro.SecurityUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 首页管理控制器
 * Created by alan on 2017/3/9.
 */
@Controller
@RequestMapping("/admin")
public class DashboardController extends SupportController{

    private static Logger logger = LoggerFactory.getLogger(DashboardController.class);

    @Autowired
    private PermissionExtraMapper permissionExtraMapper;

    /**
     * 首页
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/dashboard")
    public String dashboard(HttpServletRequest request, ModelMap modelMap, HttpSession session) {
        return "admin/dashboard";
    }


    /**
     * 登录页
     * @return
     */
    @RequestMapping("/login")
    public String login(){
        return "admin/modules/home/login";
    }

    /**
     * 登录
     * @return
     */
    @RequestMapping("/signIn")
    @ResponseBody
    @Invoke(logType = LogType.SYSTEM_LOG)
    public Object signIn(HttpServletRequest request){
        BaseResponse baseResponse = new BaseResponse();
        String errMsg = String.valueOf(request.getAttribute(FormAuthenticationFilter.DEFAULT_MESSAGE_PARAM));
        if(StringUtils.isNotBlank(errMsg) && errMsg != "null"){
            baseResponse.setCode(ResponseCode.UNKNOWN_USER);
            baseResponse.setMsg(errMsg);
            return baseResponse;
        }
        if(SecurityUtils.getPrincipal() != null){
            return baseResponse;
        }
        baseResponse.setCode(ResponseCode.UNKNOWN_USER);
        return baseResponse;
    }

    /**
     * 退出
     * @return
     */
    @RequestMapping("/logout")
    public String logout(){
        if(SecurityUtils.getPrincipal() != null){
            SecurityUtils.getSubject().logout();
        }
        return "admin/modules/home/login";
    }

}
